package com.huaxia.day16;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOTest {
    public static void main(String[] args) {
        try{
            rwNio();
         }catch (Exception e){
          e.printStackTrace();
        }
    }

    public static void rwNio() throws IOException {
        File in = new File("D:\\java面向对象\\工具\\ideaIC-2019.1.2.win.zip");
        FileInputStream fis = new FileInputStream(in);
        FileOutputStream fos = new FileOutputStream(new File("D:\\1.zip"));
        FileChannel inc = fis.getChannel();
        FileChannel outc = fos.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (true) {
            buffer.clear();
            int r = inc.read(buffer);
            if (r == -1) {
                break;
            }
            //将limit置位position位置，将position置0，并清除mark
            buffer.flip();
            outc.write( buffer );
        }
        fis.close();
        fos.close();
        inc.close();
        outc.close();

    }
}
